﻿using AutoMapper;
using MediatR;
using TraceabilityApp_Api.Application.Command.DeepProcessing_深加工_;
using TraceabilityApp_Domain;
using TraceabilityApp_ErrorCode;
using TraceabilityApp_Infrastructure;

namespace TraceabilityApp_Api.Application.Handler.DeepProcessing_深加工_
{
    public class AddSupplierHandler : IRequestHandler<AddSupplierCommand, ApiResult<int>>
    {
        private readonly IBase<SupplierModel> suppliterRepo;
        private readonly IMapper mapper;

        public AddSupplierHandler(IBase<SupplierModel> suppliterRepo,IMapper mapper)
        {
            this.suppliterRepo = suppliterRepo;
            this.mapper = mapper;
        }
        public Task<ApiResult<int>> Handle(AddSupplierCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result=new ApiResult<int>();
            var suppliter=mapper.Map<SupplierModel>(request);
            var r=suppliterRepo.Add(suppliter).Result;
            if(r>0)
            {
                result.Data = r;
                result.Code=ApiEnum.OK;
                result.Msg = "深加工供应商信息添加成功";
            }
            else
            {
                result.Data = r;
                result.Code = ApiEnum.Fail;
                result.Msg = "深加工供应商信息添加失败";
            }
            return Task.FromResult(result);
        }
    }
}
